package com.mlethe.android.demo.recyclerview.adapter;

import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.mlethe.android.demo.R;
import com.mlethe.library.recyclerview.snaphelper.GridPagerSnapHelper;
import com.mlethe.library.utils.app.DensityUtil;

import java.util.ArrayList;
import java.util.List;

public class ViewPagerActivity extends AppCompatActivity {

    private RecyclerView recyclerView;
    private ViewPagerAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_view_pager);
        recyclerView = findViewById(R.id.recyclerView);
        int row = 3;
        int column = 2;
        GridLayoutManager manager = new GridLayoutManager(getApplicationContext(), row, GridLayoutManager.HORIZONTAL, false);
        recyclerView.setLayoutManager(manager);
        adapter = new ViewPagerAdapter();
        adapter.setColumn(column).setRow(row).setPageSize(DensityUtil.getWidth(this)).setPageMargin((int) DensityUtil.dp2px(this,10));
        adapter.setOnItemClickListener((v, item, position) -> {
            Toast.makeText(this, "item->" + position, Toast.LENGTH_SHORT).show();
        });
        recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                int firstItemPosition = manager.findFirstVisibleItemPosition();
                Log.e("TAG", "onScrolled: " + dx + "    " + (firstItemPosition / 3));
            }
        });
        recyclerView.setAdapter(adapter);
        GridPagerSnapHelper pagerSnapHelper = new GridPagerSnapHelper().setRow(row).setColumn(column);
        pagerSnapHelper.attachToRecyclerView(recyclerView);
        recyclerView.postDelayed(new Runnable() {
            @Override
            public void run() {
                adapter.setNewDataFillEmpty(getData(100, 1));
            }
        }, 100);
    }

    public List<String> getData(int index, int type) {
        List<String> list = new ArrayList<>();
        for (int i = 0; i < index; i++) {
            if (type == 1) {
                list.add("刷新" + (i + 1));
            } else if (type == 2){
                list.add("重新加载" + (i + 1));
            } else {
                list.add("加载" + (i + 1));
            }
        }
        return list;
    }
}
